﻿using FluentModbus;
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace GateWayWriter
{
    public partial class 网关下发数据模拟器 : Form
    {
        public 网关下发数据模拟器()
        {
            InitializeComponent();
        }

        private int c1;
        private int c2;
        private int leak;

        private ModbusTcpClient modbusClient; // 改成 ModbusTcpClient
        private byte SLAVE_ID = 17;           // 从站地址

        private void Form1_Load(object sender, EventArgs e)
        {

            // 添加事件处理程序
            checkBox9.CheckedChanged += checkBox9_CheckedChanged;
            checkBox10.CheckedChanged += checkBox10_CheckedChanged;

            checkBox10.Checked=true;

            c1 = 0;
            textBox1.Text = c1.ToString() + "A";
            c2 = 0;
            textBox2.Text = c2.ToString() + "A";

            // 创建 ModbusTcpClient 实例
            modbusClient = new ModbusTcpClient(); // 使用无参数构造函数
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            button1.ForeColor = Color.Green;
            // 连接到目标 IP 地址，并设置端口
            modbusClient.Connect("192.168.1.122", ModbusEndianness.BigEndian);  // 端口可能会自动设置，或者你需要使用其他方式设置端口
        }

        private void checkBox9_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox9.Checked)
            {
                // 当 checkBox9 被勾选时，确保 checkBox10 取消勾选
                checkBox10.Checked = false;
            }
        }

        private void checkBox10_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox10.Checked)
            {
                // 当 checkBox10 被勾选时，确保 checkBox9 取消勾选
                checkBox9.Checked = false;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (pictureBox1.Visible)
            {
                pictureBox1.Visible = false;
            }
            else
            {
                pictureBox1.Visible = true;
            }

            if (checkBox10.Checked)
            {
                c1 = -trackBar1.Value;
                textBox1.Text = c1.ToString() + "A";
                c2 = -trackBar1.Value;
                textBox2.Text = c2.ToString() + "A";
            }
            else if (checkBox9.Checked)
            {
                c1 = trackBar1.Value;
                textBox1.Text = c1.ToString() + "A";
                c2 = trackBar1.Value;
                textBox2.Text = c2.ToString() + "A";
            }
            else
            {
                c1 = 0;
                textBox1.Text = c1.ToString() + "A";
                c2 = 0;
                textBox2.Text = c2.ToString() + "A";
            }

            leak = 0;

            for (int i = 0; i < 8; i++)
            {
                CheckBox checkbox = Controls.Find("checkbox" + (i + 1), true).FirstOrDefault() as CheckBox;
                if (checkbox != null && checkbox.Checked)
                {
                    leak |= (1 << i);  // 设置相应的位，i 对应的位
                }
            }

            if (leak > 0)
            {
                // leak 变量中每一位的状态就根据 checkbox 的选中状态进行了更新
                Console.WriteLine("Leak variable value: " + leak);
            }

            // 写入多个寄存器
            short[] registerData = new short[] { (short)leak, 0x07, (short)(c1 * 10), (short)(c2 * 10), 1, 1, 1, 1 };
            ushort startAddress = 400;  // 起始寄存器地址

            // 向从站写寄存器
            try
            {
                modbusClient.WriteMultipleRegisters(SLAVE_ID, startAddress, registerData);
                Console.WriteLine("Successfully wrote to registers.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to write to registers: " + ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
